home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
wasm201.arc
/
DOS.MAC
< prev
next >
Wrap
Text File
|
1988-07-17
|
35KB
|
858 lines
List-
;============================================================================;
; Dos Macro Interface ;
; ;
; The following macros provide an interface with the operating system (DOS). ;
; Routines are provided here for accessing the keyboard, screen, files, and ;
; miscellaneous system utilities. MS or PC DOS 2.0 or higher is required for ;
; most of the operations. The file BIOS.MAC provides lower lever routines ;
; that allow greater control over similiar types of i/o, independent of DOS. ;
; ;
; Parameters may be passed as registers, memory operands, immediate data, ;
; and strings. BP should never be used to pass parameters. Each macro ;
; describes its own parameters. Only the segement registers are guaranteed ;
; to be preserved, unless specified otherwise. The macros do not check to ;
; see if their arguments are valid. ;
; ;
; There are two different recognized formats for strings, a series of ASCII ;
; characters preceded a length byte, and a series of ASCII characters ;
; terminated by a byte 00. The first format is used for input and output. ;
; The second format is used to pass strings to DOS for opening and closing ;
; files. Literal strings that are passed as arguments are automatically ;
; declared using the appropriate format. ;
; ;
; All user i/o is done through the standard output device, i.e. it can be ;
; redirected. ;
; ;
; DOS functions return return the carry flag set if there was some error ;
; during processing (like trying to delete a file that doesn't exist). ;
; Unless specified otherwise, the macros will return the flags and registers ;
; as set by DOS. ;
; ;
; The macros IF_EXIST, STRING_OFFSET, and STRING_OFFSETZ are required, and ;
; can be found in the file MISC.MAC. ;
; ;
; The defined macros are: ;
; ;
; INPUT_CHAR input a character ;
; INPUT_HIDDEN input a character without echo ;
; INPUT_STRING input a string ;
; KEYBOARD_STATUS get input status ;
; CLEAR clear screen and home cursor (1) ;
; LOCATE set cursor position (1) ;
; HOME cursor home (1) ;
; LEFT cursor left (1) ;
; RIGHT cursor right (1) ;
; UP cursor up (1) ;
; DOWN cursor down (1) ;
; BELL sound speaker ;
; LINE start a new display line ;
; DISPLAY_CHAR output a character ;
; DISPLAY_STRING output a string ;
; DISPLAY_LINE output a string and start a new line ;
; NORMAL set display attribute to normal (1) (2) ;
; BOLD set display attribute to intense (1) (2) ;
; UNDERLINE set display attribute to underline (1) (2) ;
; BLINK set display attribute to blinking (1) (2) ;
; REVERSE set display attribute to reverse video (1) (2) ;
; OPEN open file ;
; CREATE create (truncate) file ;
; DELETE delete file ;
; WRITE write to file ;
; READ read from file ;
; SEEK move a file read/write pointer ;
; SIZE return the size of a file ;
; CLOSE close file ;
; TRAP_BREAK trap user break ;
; SET_INTERRUPT set interrupt vector ;
; GET_INTERRUPT get interrupt vector ;
; VERSION get DOS version ;
; KEEP program terminate but stay resident ;
; EXIT program terminate ;
; DOS_FUNCTION execute a dos function (through interrupt 21H) ;
; ;
; (1) Requires that ANSI.SYS or compatible device driver installed. ;
; (2) Display attributes are cumulative execept for resetting to normal ;
; (NORMAL); some of the attributes may be particular to certain ;
; monitors. ;
;============================================================================;
;===============================================;
; Input_Char ;
; ;
; Input a character from standard input. AL ;
; returns character. ;
; ;
; CHAR - optional storage for character: 8 bit ;
; register or memory. ;
;===============================================;
Input_Char Macro Char
Mov Ah,1 ;function number
Int 21h ;execute
If_Exist Char
Mov Char, Al ;save character
Endif
Endm
;===============================================;
; Input_Hidden ;
; ;
; Input a character from standard input. The ;
; character input is not echoed. AL returns ;
; character. ;
; ;
; CHAR - optional storage for character: 8 bit ;
; register or memory. ;
;===============================================;
Input_Hidden Macro Char
Mov Ah,8 ;function number
Int 21h ;execute
If_Exist Char
Mov Char, Al ;save character
Endif
Endm
;===============================================;
; Input_String ;
; ;
; Input a string from standard input. Zero flag ;
; is set if no string is input. The first byte ;
; is its length and it is terminated by a 00. ;
; ;
; LOCATION - storage for string location: 16 ;
; bit register or memory. ;
; MAX_LENGTH - absolute maximum length: 8 bit ;
; immediate data. ;
; INP_LENGTH - optional variable maximum ;
; length: 8 bit operand. ;
; ;
; The variable maximum length must NEVER be ;
; longer than the absolute maximum length. If ;
; no variable length is specified, the absolute ;
; maximum length will be used to limit input. ;
;===============================================;
Input_String Macro Location, Max_Length, Inp_Length
Jmps SkipBuffer
InputBuffer Label Byte
Db Max_Length + 1 ;default maximum length
Db ? ;input length
Ds Max_Length + 1 ;storage
SkipBuffer ;continue execution after buffer
If_Exist Inp_Length
Mov Al, Inp_Length ;specified length
Inc Al ;account for CR
Mov InputBuffer, Al ;save
Endif
Mov Dx, Offset InputBuffer ;formatted buffer
Mov Ah, 0ah ;function
Int 21h ;execute
Line ;next display line
Mov Bl, InputBuffer+1 ;get input length
Sub Bh, Bh ;clear upper byte
Mov Byte [Bx+InputBuffer+2], 0 ;store 00 on CR
Mov Location, Offset InputBuffer + 1 ;string body
Or Bx, Bx ;set ZF
Endm
;===============================================;
; Keyboard_Status ;
; ;
; Get the standard input status. AL returns ;
; status. Zero flag is set if there are no ;
; characters pending. ;
; ;
; STATUS - optional storage for status: 8 bit ;
; register or memory. ;
;===============================================;
Keyboard_Status Macro Status
Mov Ah,0bh ;function number
Int 21h ;execute
If_Exist Status
Mov Status, Al ;save character
Endif
Or Al, Al ;set ZF
Endm
;===============================================;
; Clear ;
; ;
; Clear the screen and home the cursor. ;
;===============================================;
Clear Macro
Jmps B ;skip control string
A Db 3,27,'[J' ;ansi control string with length
B Mov Ax, Offset A ;string location
Display_String Ax ;send control code to display
Endm
;===============================================;
; Locate ;
; ;
; Move the cursor to a specified position. ;
; ;
; ROW - row position: 8 bit register or ;
; immediate data. ;
; COLUMN - column position: 8 bit register or ;
; immediate data. ;
; ;
; The row and column number start at one, i.e. ;
; the upper left position is row one and column ;
; one. ;
;===============================================;
Locate Macro Row, Column
Push Ax
Push Ax
Mov Bp, Sp
Mov Byte [Bp+2], Row ;set row
Mov Byte [Bp], Column ;set column
Home ;move to upper left corner
Pop Cx
Sub Ch, Ch ;only a byte
Dec Cx ;make scale 0 to X
Jz B ;jump if first column
A Push Cx
Right ;move right
Pop Cx
Loop A ;loop back for each column
B Pop Cx
Sub Ch, Ch ;only a byte
Dec Cx ;make scale 0 to X
Jz C ;jump if first row
D Push Cx
Down ;move down
Pop Cx
Loop D ;loop back for each row
C
Endm
;===============================================;
; Home ;
; ;
; Move the cursor to the upper left corner. ;
;===============================================;
Home Macro
Jmps B ;skip control string
A Db 6,27,'[1;1H' ;ansi control string with length
B Mov Ax, Offset A ;string location
Display_String Ax ;send control code to display
Endm
;===============================================;
; Left ;
; ;
; Move the cursor left one column. ;
;===============================================;
Left Macro
Jmps B ;skip control string
A Db 3,27,'[D' ;ansi control string with length
B Mov Ax, Offset A ;string location
Display_String Ax ;send control code to display
Endm
;===============================================;
; Right ;
; ;
; Move the cursor right one column. ;
;===============================================;
Right Macro
Jmps B ;skip control string
A Db 3,27,'[C' ;ansi control string with length
B Mov Ax, Offset A ;string location
Display_String Ax ;send control code to display
Endm
;===============================================;
; Up ;
; ;
; Move the cursor up one row. ;
;===============================================;
Up Macro
Jmps B ;skip control string
A Db 3,27,'[A' ;ansi control string with length
B Mov Ax, Offset A ;string location
Display_String Ax ;send control code to display
Endm
;===============================================;
; Down ;
; ;
; Move the cursor down one row. ;
;===============================================;
Down Macro
Jmps B ;skip control string
A Db 3,27,'[B' ;ansi control string with length
B Mov Ax, Offset A ;string location
Display_String Ax ;send control code to display
Endm
;===============================================;
; Bell ;
; ;
; Sound the speaker. ;
;===============================================;
Bell Macro
Display_Char 7 ;print a BEL
Endm
;===============================================;
; Line ;
; ;
; Start a new diplay line. ;
;===============================================;
Line Macro
Display_Char 13 ;carriage return
Display_Char 10 ;linefeed
Endm
;===============================================;
; Display_Char ;
; ;
; Display a character to standard output. ;
; ;
; CHAR - character to display: 8 bit operand. ;
;===============================================;
Display_Char Macro Char
Mov Dl, Char ;character
Mov Ah, 2 ;function number
Int 21h ;execute
Endm
;===============================================;
; Display_String ;
; ;
; Display a string to standard output. The ;
; first byte must be its length. ;
; ;
; STRING - string to display: literal string or ;
; 16 bit location of string. ;
;===============================================;
Display_String Macro String
String_Offset String, Bx ;get string offset
Mov Cl, [Bx] ;get length byte
Sub Ch, Ch ;clear upper byte, CX=length
Mov Dx, Bx
Inc Dx ;start of string body
Mov Bx, 1 ;standard output
Mov Ah, 40h ;function number
Int 21h ;execute
Endm
;===============================================;
; Display_Line ;
; ;
; Display a string to standard output and then ;
; start a new line. The first byte must be its ;
; length. ;
; ;
; STRING - string to display: literal string or ;
; 16 bit location of string. ;
;===============================================;
Display_Line Macro String
String_Offset String, Bx ;get string offset
Mov Cl, [Bx] ;get length byte
Sub Ch, Ch ;clear upper byte, CX=length
Mov Dx, Bx
Inc Dx ;start of string body
Mov Bx, 1 ;standard output
Mov Ah, 40h ;function number
Int 21h ;execute
Line ;start a new line
Endm
;===============================================;
; Normal ;
; ;
; Set display attribute to normal. ;
;===============================================;
Normal Macro
Jmps B ;skip control string
A Db 4,27,'[0m' ;ansi control string with length
B Mov Ax, Offset A ;string location
Display_String Ax ;send control code to display
Endm
;===============================================;
; Bold ;
; ;
; Set display attribute to bold. ;
;===============================================;
Bold Macro
Jmps B ;skip control string
A Db 4,27,'[1m' ;ansi control string with length
B Mov Ax, Offset A ;string location
Display_String Ax ;send control code to display
Endm
;===============================================;
; Underline ;
; ;
; Set display attribute to underline. ;
;===============================================;
Underline Macro
Jmps B ;skip control string
A Db 4,27,'[4m' ;ansi control string with length
B Mov Ax, Offset A ;string location
Display_String Ax ;send control code to display
Endm
;===============================================;
; Blink ;
; ;
; Set display attribute to blink. ;
;===============================================;
Blink Macro
Jmps B ;skip control string
A Db 4,27,'[5m' ;ansi control string with length
B Mov Ax, Offset A ;string location
Display_String Ax ;send control code to display
Endm
;===============================================;
; Reverse ;
; ;
; Set display attribute to reverse video. ;
;===============================================;
Reverse Macro
Jmps B ;skip control string
A Db 4,27,'[7m' ;ansi control string with length
B Mov Ax, Offset A ;string location
Display_String Ax ;send control code to display
Endm
;===============================================;
; Open ;
; ;
; Open a file for reading and writing. AX ;
; returns the file handle. ;
; ;
; FILE - file to open: literal string or 16 bit ;
; location of string. ;
; HANDLE - optional storage for file handle: 16 ;
; bit register or memory. ;
;===============================================;
Open Macro File, Handle
String_Offsetz File, Dx ;get filename offset
Mov Al, 00000010b ;read/write status
Mov Ah, 3dh ;function number
Int 21h ;execute
If_Exist Handle
Mov Handle, Ax ;save handle
Endif
Endm
;===============================================;
; Create ;
; ;
; Create a file for reading and writing. AX ;
; returns the file handle. ;
; ;
; FILE - file to create: literal string or 16 ;
; bit location of string. ;
; HANDLE - optional storage for file handle: 16 ;
; bit register or memory. ;
;===============================================;
Create Macro File, Handle
String_Offsetz File, Dx ;get filename offset
Sub Cx, Cx ;normal attribute
Mov Ah, 3ch ;function number
Int 21h ;execute
If_Exist Handle
Mov Handle, Ax ;save handle
Endif
Endm
;===============================================;
; Delete ;
; ;
; Delete a file. ;
; ;
; FILE - file to delete: literal string or 16 ;
; bit location of string. ;
;===============================================;
Delete Macro File
String_Offsetz File, Dx ;get filename offset
Mov Ah, 41h ;function number
Int 21h ;execute
Endm
;===============================================;
; Write ;
; ;
; Write to a file. AX returns the number of ;
; bytes written. ;
; ;
; HANDLE - file handle: 16 bit register or ;
; immediate data. ;
; BYTES - number of bytes to write: 16 bit ;
; register or immediate data. ;
; LOCATION - location of bytes to write: 16 bit ;
; register or immediate data. ;
; RETURN - optional storage for the number of ;
; bytes written: 16 bit register or memory. ;
;===============================================;
Write Macro Handle, Bytes, Location, Return
Push Bx
Push Cx
Push Dx ;parameters on stack
Mov Bp, Sp
Mov Word [Bp+4], Handle ;handle
Mov Word [Bp+2], Bytes ;bytes to write
Mov Word [Bp], Location ;location of buffer
Pop Dx
Pop Cx
Pop Bx ;restore parameters
Mov Ah, 40h ;function number
Int 21h ;execute
If_Exist Return
Mov Return, Ax ;save bytes written
Endif
Endm
;===============================================;
; Read ;
; ;
; Read from a file. AX returns the number of ;
; bytes read. ;
; ;
; HANDLE - file handle: 16 bit register or ;
; immediate data. ;
; BYTES - number of bytes to read: 16 bit ;
; register or immediate data. ;
; LOCATION - location to put bytes read: 16 bit ;
; register or immediate data. ;
; RETURN - optional storage for the number of ;
; bytes read: 16 bit register or memory. ;
;===============================================;
Read Macro Handle, Bytes, Location, Return
Push Bx
Push Cx
Push Dx ;parameters on stack
Mov Bp, Sp
Mov Word [Bp+4], Handle ;handle
Mov Word [Bp+2], Bytes ;bytes to read
Mov Word [Bp], Location ;location of buffer
Pop Dx
Pop Cx
Pop Bx ;restore parameters
Mov Ah, 3fh ;function number
Int 21h ;execute
If_Exist Return
Mov Return, Ax ;save bytes read
Endif
Endm
;===============================================;
; Seek ;
; ;
; Move the read/write pointer of a file to a ;
; specified record location. ;
; ;
; HANDLE - file handle: 16 bit register or ;
; immediate data. ;
; RECORD_HIGH - optional high word of record ;
; number to find: 16 bit register or ;
; immediate data. ;
; RECORD_LOW - optional low word of record ;
; number to find: 16 bit register or ;
; immediate data. ;
; RECORD_SIZE - optional size of records in ;
; file: 16 bit register or immediate data. ;
; ;
; If the record high or low number is not ;
; specified, zero is substituted. If record ;
; size is not specified, one is substituted. ;
; The first record is record number zero. Using ;
; SEEK without the record size turns the record ;
; number into an absolute 32 bit offset ;
; from the beginning of the file. If only the ;
; handle is present, the SEEK will move the ;
; read/write pointer to the start of file. ;
;===============================================;
Seek Macro Handle, Record_High, Record_Low, Record_Size
Push Ax
Push Bx
Push Cx
Push Dx ;parameters on stack
Mov Bp, Sp
Mov Word [Bp+4], Handle ;handle
If_Exist Record_High
Mov Word [Bp], Record_High ;record number, high word
Else
Mov Word [Bp], 0 ;record number, high word
Endif
If_Exist Record_Low
Mov Word [Bp+6], Record_Low ;record number, low word
Else
Mov Word [Bp+6], 0 ;record number, low word
Endif
If_Exist Record_Size
Mov Word [Bp+2], Record_Size ;record size
Else
Mov Word [Bp+2], 1 ;record size
Endif
Pop Dx
Pop Cx
Pop Bx
Pop Ax ;restore parameters
Mul Ax, Cx ;get offset, DX.AX * CX
Mov Cx, Ax
Xchg Cx, Dx ;offset into CX.DX
Mov Ax, 4200h ;function number and position method
Int 21h ;execute
Endm
;===============================================;
; Size ;
; ;
; Return the size of a file and move the read/ ;
; write pointer to the end of the file. DX ;
; returns the high (most significant) word of ;
; the file size and AX returns the low (least ;
; significant) word of the file size. ;
; ;
; HANDLE - file handle: 16 bit register or ;
; immediate data. ;
; HIGH_WORD - optional storage for the high ;
; word: 16 bit register or memory. ;
; LOW_WORD - optional storage for the low word: ;
; 16 bit register or memory. ;
;===============================================;
Size Macro Handle, High_Word, Low_Word
Mov Bx, Handle ;set handle
Sub Cx, Cx
Mov Dx, Cx ;move to offset from end 0000.0000
Mov Ax, 4202h ;function number and position method
Int 21h ;execute
If_Exist High_Word
Mov High_Word, Dx ;save most significant word
Endif
If_Exist Low_Word
Mov Low_Word, Ax ;save least significant word
Endif
Endm
;===============================================;
; Close ;
; ;
; Close a file. ;
; ;
; HANDLE - file handle: 16 bit register or ;
; immediate data. ;
;===============================================;
Close Macro Handle
Mov Bx, Handle ;set handle
Mov Ah, 3eh ;function number
Int 21h ;execute
Endm
;===============================================;
; Trap_Break ;
; ;
; Set up a routine to service a system break ;
; and thus allow a program to retain control in ;
; event that the user enters a Ctrl Break. ;
; Assumes that the routine is in the DS ;
; segement. The modified interrupt vector (23H) ;
; is automatically restored by the system when ;
; the program terminates (i.e. it does not have ;
; to be saved and later restored). ;
; ;
; ROUTINE - routine to handle break: routine ;
; name (far label) or 16 bit register or ;
; immediate data. ;
;===============================================;
Trap_break Macro Routine
Dummy Label Far ;this is only for testing purposes
If Type(Routine) And Type(Dummy)
Set_Interrupt 23h, Ds, Offset Routine ;set interrupt
Else
Set_Interrupt 23h, Ds, Routine ;set interrupt
Endif
Endm
;===============================================;
; Set_Interrupt ;
; ;
; Set the specified interrupt vector. DS is ;
; destroyed if the segment is different than ;
; the initial DS value. ;
; ;
; INT_NUM - interrupt number to set: 8 bit ;
; register or immediate data. ;
; SEGMENT - segement value: 16 bit register or ;
; immediate data. ;
; OFFSET - offset value: 16 bit register or ;
; immediate data. ;
;===============================================;
Set_Interrupt Macro Int_Num, Segment, Offset
Push Ax
Push Ds
Push Dx ;parameters on stack
Mov Bp, Sp
Mov Byte [Bp+4], Int_Num ;vector to set
Mov Word [Bp+2], Segment ;segment
Mov Word [Bp], Offset ;offset
Pop Dx
Pop Ds
Pop Ax ;restore parameters
Mov Ah, 25h ;function number
Int 21h ;execute
Endm
;===============================================;
; Get_Interrupt ;
; ;
; Get the segement and offset of the specified ;
; interrupt number. ES:BX return the location ;
; (ES is destroyed). ;
; ;
; INT_NUM - interrupt number to get: 8 bit ;
; register or memory. ;
; SEGMENT - optional storage for segement: 16 ;
; bit register or memory. ;
; OFFSET - optional storage for offset: 16 bit ;
; register or memory. ;
;===============================================;
Get_Interrupt Macro Int_Num, Segment, Offset
Mov Al, Int_Num ;vector to set
Mov Ah, 35h ;function number
Int 21h ;execute
If_Exist Segment
Mov Segment, Es ;save segment
Endif
If_Exist Offset
Mov Offset, Bx ;save offset
Endif
Endm
;===============================================;
; Version ;
; ;
; Return the present MS/PC DOS version number. ;
; AL returns the primary version, AH returns ;
; the secondary version. The version is zero if ;
; pre-DOS 2.0. ;
; ;
; VERSION - optional storage for the entire ;
; version word: 16 bit register or memory. ;
;===============================================;
Version Macro Version
Mov Ah, 30h ;function number
Int 21h ;execute
If_Exist Version
Mov Version, Ax ;save
Endif
Endm
;===============================================;
; Keep ;
; ;
; Program terminate and stay resident. ;
; ;
; SIZE - number of paragraphs to reserve: 16 ;
; bit register or immediate data. ;
; ERR_CODE - optional error code: 8 bit ;
; register or immediate data. ;
; ;
; An error code of zero is returned if none is ;
; specified. ;
;===============================================;
Keep Macro Size, Err_Code
Push Ax
Push Dx
Mov Bp, Sp
Mov Word [Bp], Size ;paragraphs to reserve
If_Exist Err_Code
Mov Byte [Bp+2], Err_Code ;set error code
Else
Mov Byte [Bp+2], 0 ;error code zero
Endif
Pop Dx
Pop Ax
Mov Ah, 31h ;function number
Int 21h ;execute
Endm
;===============================================;
; Exit ;
; ;
; Program termination. ;
; ;
; ERR_CODE - optional error code: 8 bit ;
; operand. ;
; ;
; An error code of zero is returned if none is ;
; specified. ;
;===============================================;
Exit Macro Err_Code
If_Exist Err_Code
Mov Al, Err_Code ;set error code
Else
Sub Al, Al ;error code zero
Endif
Mov Ah, 4ch ;function number
Int 21h ;execute
Endm
;===============================================;
; Dos_Function ;
; ;
; Execute a DOS function with a subfunction. ;
; The function and subfunction are the values ;
; that are to be placed in AH and AL ;
; respectively. ;
; ;
; FUNC_NUM1 - main function (AH): 8 bit ;
; register or immediate data. ;
; FUNC_NUM2 - optional secondary fuction (AL): ;
; 8 bit register or immediate data. ;
;===============================================;
Dos_Function Macro Func_Num1, Func_Num2
Push Ax ;parameters on stack
Mov Bp, Sp
Mov Byte [Bp+1], Func_Num1 ;set function (AH)
If_Exist Func_Num2
Mov Byte [Bp], Func_Num2 ;set subfunction (AL)
Endif
Pop Ax ;restore parameters
Int 21h ;execute
Endm